<template>
  <el-dialog
    class="troila-dialog"
    title="修改密码"
    :visible.sync="dialogFormVisible"
    :modal-append-to-body="true"
    :append-to-body="true"
    width="600px"
  >
    <el-form
      class="troila-form"
      :label-width="formLabelWidth"
      :model="form"
      :rules="rules"
      ref="form"
    >
      <el-form-item label="原密码" prop="oldPassword">
        <el-input
          v-model="form.oldPassword"
          autocomplete="off"
          show-password
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          v-model="form.newPassword"
          autocomplete="off"
          type="password"
          show-password
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="newPassword_">
        <el-input
          v-model="form.newPassword_"
          autocomplete="off"
          type="password"
          show-password
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item
        label=""
        style="padding: 0; color: #e73c3c; margin-bottom: -10px;"
      >
        <i class="el-icon-warning-outline" style="padding-right: 4px;"></i
        >密码修改成功后，您将需要重新登录
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">{{
        ajaxIng ? '关 闭' : '取 消'
      }}</el-button>
      <el-button type="primary" :loading="ajaxIng" @click="submitForm('form')"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
export default {
  name: 'passwordForm',
  props: {
    value: {
      // row
      type: [Boolean, Object],
      default: false,
    },
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入新密码'))
      } else if (!value) {
        callback(new Error('新密码不能为空'))
      } else if (value.length < 6) {
        callback(new Error('密码不得少于6位'))
      } else {
        callback()
      }
    }
    const validatePassword2 = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请确认密码'))
      } else if (value.length < 6) {
        callback(new Error('密码不得少于6位'))
      } else if (this.form.newPassword != value) {
        callback(new Error('两次密码输入不一致'))
      } else {
        callback()
      }
    }
    return {
      dialogFormVisible: this.value ? true : false, //弹窗显示
      ajaxIng: false,
      form: {
        oldPassword: '',
        newPassword: '',
        newPassword_: '',
      },
      formLabelWidth: '120px', //标题文字宽度
      rules: {
        // 规则
        oldPassword: [
          { required: true, message: '请输入原始密码', trigger: 'blur' },
        ],
        newPassword: [
          { required: true, trigger: 'blur', validator: validatePassword },
        ],
        newPassword_: [
          { required: true, trigger: 'blur', validator: validatePassword2 },
        ],
      },
    }
  },
  watch: {
    value(v) {
      let show = v ? true : false
      this.dialogFormVisible !== show ? (this.dialogFormVisible = show) : null
      for (let k in this.form) {
        typeof this.form[k] == 'string' || typeof this.form[k] == 'number'
          ? (this.form[k] = '')
          : (this.form[k] = [])
      }
      this.form.roleId = v.roles
        ? v.roles.map((v) => {
            return v.id
          })
        : null
      this.$nextTick(() => {
        this.$refs.form ? this.$refs.form.clearValidate() : null
      })
      show ? (this.ajaxIng = false) : null
    },
    dialogFormVisible(v) {
      v ? null : this.$emit('input', v)
    },
  },
  created() {},
  methods: {
    submitForm(formName) {
      if (this.ajaxIng) {
        return
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            oldPassword: this.form.oldPassword,
            newPassword: this.form.newPassword,
          }
          this.ajaxIng = true
          this.API.user
            .updatePassword(data)
            .then((res) => {
              this.$message.success('修改成功，请重新登录')
              this.dialogFormVisible = false
              this.$store
                .dispatch('user/logout')
                .then(() => {})
                .catch(() => {})
              this.$router.replace('/login')
              // this.$message.success('修改成功！')
              // this.dialogFormVisible = false
            })
            .catch((res) => {
              this.ajaxIng = false
            })
        } else {
          // console.log('error submit!!')
          this.ajaxIng = false
          return false
        }
      })
    },
  },
}
</script>